$(function(){
    //$('nav#menu_left').mmenu();
    var now = new Date();
    var optdatetime = {
        minDate: new Date(now.getFullYear(), now.getMonth(), now.getDate()),
        maxDate:new Date(2018, now.getMonth(), now.getDate()),
        showNow:false,
        preset: 'datetime',
        theme: 'android-ics light',
        lang: 'zh',
        display: 'modal',
        invalid: [ 'w0', 'w6', '5/1' ],
        mode: 'scroller',
        onSelect: function(time){
            var date = new Date(Date.parse(time.replace(/-/g, '/')));
            $('#startTime_show').val(date.format("Y年MM月dd日 hh点mm分")+' (周'+date.getZhDay(date)+')')
        }
    };
    $('#startTime').mobiscroll(optdatetime);
    $('#startTime_show').click(function(){
        $('#startTime').mobiscroll('show');
        return false;
    });
    // more
    $('#showMore').on('click', function(){
        var div = $('#more_info');
        if(div.css('display')==='none'){
            div.slideDown()
        }else{
            div.slideUp()
        }
    })
    $('#actTypeBtns label').on('click', function(e){
        var val = $(this).children().val();
        if(val === 'kecheng'){
            $('#actlocation').val('线上课堂');
            $('#remark').val('请大家准时参与课程，积极踊跃发言')
        }else if(val === 'jiaoliu'){
            $('#remark').val('请大家尽情分享心得')
        }else{
            $('#remark').val('圈主买单，大家表客气，不醉不归~')
        }
    })
    // 提交
    $('#submit').on('click', function(){
        var btn = $(this);
        var actname = $.trim($('#actname').val()),
            startTime = $.trim($('#startTime').val()),
            actlocation = $.trim($('#actlocation').val()),
            remark = $.trim($('#remark').val()),
            factionId = $('#factionId').val(),
            acttype = $('input[name="acttype"]:checked').val() || 'kecheng',
            actscore = $.trim($('#actscore').val()) || 10;
        var actId = $('#actId').val();
        if(actname === ''){
            $('#actname').parent().addClass('has-error');
            AlertBox.showTip('来个主题');
            return;
        }
        $('#actname').parent().removeClass('has-error')
        btn.button('loading');
        $.post('/saveAct', {
            _csrf: $('#_csrf').val(),
            actid: actId,
            actname: actname,
            startTime: startTime,
            actlocation: actlocation,
            factionId: factionId,
            remark: remark,
            acttype: acttype,
            actscore: actscore
        }, function(res){
            if(res.error){
                AlertBox.showTip('出问题了...');
                btn.button('reset');
            }else{
                window.location.href = '/invite/'+res.data._id;
                /*AlertBox.showConfirm('伙伴们已经入座？返回快乐邦公众号获取聚会暗号，让伙伴们对号入局～'
                    ,{ok:'获取暗号',cancel:'取消'}, function(){
                        WeixinJSBridge.call('closeWindow');
                    },function(){
                        window.location.href = '/invite/'+res.data._id;
                    })*/
            }
        });
    })

})